# -*- coding: utf-8 -*-
import scrapy
import random

from XCFScrapy.items import foodmaterialCategroyItem

class FoodmaterialspiderSpider(scrapy.Spider):
    name = 'foodmaterialspider'
    allowed_domains = ['xiachufang.com']
    start_urls = ['http://xiachufang.com/']

    def parse(self, response):

        #大类
        menu_list = response.xpath("//div/ul[@class='plain homepage-cats']/li/a")
        for menu in menu_list:
            name = menu.xpath("./span/text()").extract_first("")
            url = menu.xpath("./attribute::href").extract_first("")

            prentItem = foodmaterialCategroyItem()
            prentItem['name'] = name
            prentItem['url'] = "http://www.xiachufang.com"+url
            prentItem['foodId'] = self.generate_code()
            prentItem['prentFoodId'] = "0"

            # 获取食材分类下的子分类
            submenu_list = menu.xpath("../div/div[@class='submenu-item clearfix pure-g bottom-line']")
            for submenu in submenu_list:
                subname = submenu.xpath("./div[1]/a/text()").extract_first("")
                suburl = submenu.xpath("./div[1]/a/attribute::href").extract_first("")

                subtItem = foodmaterialCategroyItem()
                subtItem['name'] = subname
                subtItem['url'] = "http://www.xiachufang.com" + suburl
                subtItem['foodId'] = self.generate_code()
                subtItem['prentFoodId'] = prentItem['foodId']
                yield subtItem

                # 子分类下还有子分类
                submenu_list1 = submenu.xpath("./div[2]/span")
                submenu_list1.pop()
                for submenu1 in submenu_list1:
                    subname1 = submenu1.xpath("./a/text()").extract_first("")
                    suburl1 = submenu1.xpath("./a/attribute::href").extract_first("")

                    subtItem1 = foodmaterialCategroyItem()
                    subtItem1['name'] = subname1
                    subtItem1['url'] = "http://www.xiachufang.com" + suburl1
                    subtItem1['foodId'] = self.generate_code()
                    subtItem1['prentFoodId'] = subtItem['foodId']
                    yield subtItem1

            yield prentItem

    """
    随机生成4位数
    """
    def generate_code(self):
        # 定义一个空列表，每次循环，将拿到的值，加入列表
        random_num = []
        # choice函数：每次从seeds拿一个值，加入列表
        for i in range(4):
            random_num.append(str(random.randint(0, 9)))
        return "".join(random_num)

